package cc.rengu.igas.bsps.core.service.trans;

import cc.rengu.oltp.service.base.DubboAsynBaseService;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.dao.PayOrderInfoMapper;
import cc.rengu.oltp.service.common.dao.impl.PayOrderInfoMapperImpl;
import cc.rengu.oltp.service.common.entity.PayOrderInfo;
import cc.rengu.oltp.service.common.enums.ChannelEnum;
import cc.rengu.oltp.utility.annotation.BIZ_PROCESS;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import com.alibaba.fastjson.JSONObject;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 虚拟账户充值异步冲正
 */
public class AsynCoreRevsalService extends DubboAsynBaseService {

    @BIZ_PROCESS
    public void coreRevsal() throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String sysNo = xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.SYS_SEQ_NUM);
        String instId = xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.INST_ID);
        PayOrderInfoMapper payOrderInfoMapper = new PayOrderInfoMapperImpl();
        List<PayOrderInfo> payOrderInfoList = payOrderInfoMapper.selectPayOrderInfoBySysSeqNum(instId, sysNo);
        if (payOrderInfoList == null || payOrderInfoList.isEmpty()) {
            rglog.error("根据法人行号<{}>,系统流水号<{}>获取支付订单列表失败", instId, sysNo);
            return;
        }
        List<PayOrderInfo> payOrderInfoListEsb = payOrderInfoList.stream().filter(item -> item.getMsgDstId().equals(ChannelEnum.ESB.getChannelType())).collect(Collectors.toList());
        if (payOrderInfoListEsb == null || payOrderInfoListEsb.isEmpty()) {
            rglog.error("根据法人行号<{}>,系统流水号<{}>获取核心扣款支付订单流水失败", instId, sysNo);
            return;
        }
        PayOrderInfo esbPayOrderInfo = payOrderInfoListEsb.get(0);
        /*判断记账结果，更新支付订单状态*/
        if (!xmlTreeUtil.isExist(TreeNodeConstant.DUBBO_RESPONSE)) {
            rglog.error("dubbo调用失败");
            return;
        }
        rglog.debug("dubbo调用正常返回");
        String responseStr = xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.DUBBO_RESPONSE);
        cc.rengu.igas.acct.facade.result.Result<JSONObject> responseResult = (cc.rengu.igas.acct.facade.result.Result<JSONObject>) xmlTreeUtil.getXmlTreeObjectValue(TreeNodeConstant.DUBBO_RESPONSE, cc.rengu.igas.acct.facade.result.Result.class);
        if (!responseResult.isSuccess()) {
            rglog.error("dubbo调用失败");
            /*更新支付订单表失败*/
            return;
        }
//      CoreGeneralAcctRevsalResponse response = JSON.toJavaObject(responseResult.getResult(), CoreGeneralAcctRevsalResponse.class);

        /*判断冲正结果，更新状态*/
//        if ( !BspsAppParamConstant.DUBBO_SUCCESS.equals(response.getRespCode())) {
//            rglog.error("核心冲正接口调用失败");
//        } else {
//            esbPayOrderInfo.setBizStatus(BizStatusEnum.REVSAL.getStatus());
//            esbPayOrderInfo.setUpdateTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));
//            OrderService orderService = new OrderServiceImpl();
//            if (orderService.updateOrderInfo(null, null, esbPayOrderInfo)) {
//                rglog.info("更新核心支付订单信息成功");
//            } else {
//                rglog.error("更新核心支付订单信息失败");
//            }
//        }

    }
}
